<template>
  <div class="login-container">
    <div class="login-box">
      <img class="login-logo" src="/img/icons/logon.png" alt="系统logo">
      <div class="login-form">
        <h2 class="title">后台管理系统</h2>
        <p class="subtitle">MANAGEMENT SYSTEM</p>

        <el-form ref="form" :model="form" :rules="rules">
          <el-form-item prop="username">
            <el-input v-model="form.username" placeholder="用户名" prefix-icon="el-icon-user" />
          </el-form-item>

          <el-form-item prop="password">
            <el-input v-model="form.password" placeholder="密码" type="password" prefix-icon="el-icon-lock"
              show-password />
          </el-form-item>

          <el-button type="primary" class="login-btn" :loading="loading" @click="handleLogin">
            登录
          </el-button>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  mixins: [http()],
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
        ]
      },
      loading: false
    }
  },
  methods: {
    async handleLogin() {
      try {
        await this.$refs.form.validate()
        this.loading = true

        const res = await this.$httpPost('/login', this.form)
        localStorage.setItem('token', res.data.token)

        this.$router.push('/list')
        this.$message.success('登录成功')
      } catch (error) {
        this.$handleError(error)
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  @apply flex items-center justify-center min-h-screen bg-gray-100;

  .login-box {
    @apply flex bg-white rounded-lg shadow-lg overflow-hidden w-4/5 max-w-4xl;

    .login-logo {
      @apply w-1/2 h-auto object-cover;
    }

    .login-form {
      @apply p-12 w-1/2;

      .title {
        @apply text-3xl font-bold text-gray-800 mb-2;
      }

      .subtitle {
        @apply text-gray-500 text-lg mb-8;
      }

      .login-btn {
        @apply w-full mt-4;
      }
    }
  }
}
</style>